package com.hpxx.ylzswl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.alibaba.fastjson.JSON;
import com.hpxx.ylzswl.R;
import com.hpxx.ylzswl.adapter.MessageAdapter;
import com.hpxx.ylzswl.base.BaseActivity;
import com.hpxx.ylzswl.base.BaseEvent;
import com.hpxx.ylzswl.bean.MessageBean;
import com.hpxx.ylzswl.event.NoticeEvent;
import com.hpxx.ylzswl.https.HttpAddress;
import com.hpxx.ylzswl.https.RequestParams;
import com.hpxx.ylzswl.info.ManageInfo;
import com.hpxx.ylzswl.views.NoScrollingListView;
import com.universal.frame.generalutils.GeneralUtil;
import com.universal.frame.generalutils.JsonUtil;
import com.universal.frame.generalutils.ToastUtil;
import com.universal.frame.okhttp.OKHttpUtils;
import com.universal.frame.okhttp.ResultCallBack;
import com.universal.frame.pulltorefresh.PullToRefreshBase;
import com.universal.frame.pulltorefresh.PullToRefreshScrollView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import okhttp3.Request;

/**
 * Created by Administrator on 2017/11/9.
 */

public class MessageActivity extends BaseActivity implements
        PullToRefreshBase.OnRefreshListener2,ResultCallBack{

    private PullToRefreshScrollView pull_refresh_scrollview;
    private NoScrollingListView nsl_list;
    private List<MessageBean> list = new ArrayList<MessageBean>();
    private MessageAdapter adapter;
    private int pageIndex = 0;
    private boolean toRefresh = true;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messgae);
        back();
        setTitle("消息中心");
    }

    @Override
    public void initView() {
        pull_refresh_scrollview = (PullToRefreshScrollView) findViewById(R.id.pull_refresh_scrollview);
        nsl_list = (NoScrollingListView) findViewById(R.id.nsl_list);
        messageListPost(pageIndex);
        initScrollView();
    }
    private void messageListPost(int pageIndex){
        OKHttpUtils.postAsync(this
                , HttpAddress.NOTICE_LIST_URL
                , new RequestParams(this).getMessageListParams(pageIndex, 3), this
                ,true,1);
    }
//    private void NoticeReadPost(String noticeId){
//        OKHttpUtils.postAsync(this
//                , HttpAddress.NOTICE_UPDATARED_URL
//                , new RequestParams(this).getMessageUpdataParams(noticeId), this
//                ,false,2);
//    }
    public void initScrollView() {
        this.pull_refresh_scrollview.getLoadingLayoutProxy(false, true)
                .setPullLabel(
                        this.getResources().getString(
                                R.string.pull_to_refresh_pull_label_up));
        pull_refresh_scrollview.getLoadingLayoutProxy(false, true)
                .setRefreshingLabel(
                        this.getResources().getString(
                                R.string.pull_to_refresh_refreshing_label));
        pull_refresh_scrollview.getLoadingLayoutProxy(false, true)
                .setReleaseLabel(
                        this.getResources().getString(
                                R.string.pull_to_refresh_release_label));

        pull_refresh_scrollview.setMode(PullToRefreshBase.Mode.BOTH);//both  可以上拉、可以下拉
        pull_refresh_scrollview.setOnRefreshListener(this);
//        pull_refresh_listview.setOnItemClickListener(this);

        adapter = new MessageAdapter(this,list);
        nsl_list.setAdapter(adapter);
        nsl_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(MessageActivity.this, OrderDetailsAvtivity.class);
                intent.putExtra("ORDER_ID",list.get(position).orderId);
                intent.putExtra("NOTICE_ID",list.get(position).noticeId);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        pageIndex = 0;
        messageListPost(pageIndex);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        if(toRefresh){
            pageIndex++;
            messageListPost(pageIndex);
        }else{
            ToastUtil.showToast(this,"没有更多数据");
            pull_refresh_scrollview.onRefreshComplete();
        }
    }

    @Override
    public void onSuccess(String response, int flag) {
        int status = JsonUtil.getInt(response, "status", 0);
        try {
            switch (flag) {
                case 1:
                    messageListDetail(response);
                    break;

                default:
                    break;
            }
        } catch (Exception e) {
            GeneralUtil.tryShow(this, e);
        }
    }



    private void messageListDetail(String response) {
        Log.e("lzx", "shopDetail: " + response);
        pull_refresh_scrollview.onRefreshComplete();
        int status = JsonUtil.getInt(response, "status", 0);
        if (status == 1){
            String data = JsonUtil.getString(response,"data","");
            ManageInfo info = JSON.parseObject(data, ManageInfo.class);
            Log.i("tag", "size = " + info.noticeList.size());
            if (info.noticeList != null) {
                if (info.noticeList.size() > 0) {
                    if (pageIndex == 0) {
                        list.clear();
                        list.addAll(info.noticeList);

                    } else {
                        list.addAll(info.noticeList);
                    }


                }
                adapter.notifyDataSetChanged();
                if(info.noticeList.size() < 10){
                    toRefresh = false;
                }else{
                    toRefresh = true;
                }
            }


        }else {
            String msg = JsonUtil.getString(response,"msg","");
            ToastUtil.showToast(this,msg);
        }
    }

    @Override
    public void onFailure(Request request, IOException e, int flag) {
        pull_refresh_scrollview.onRefreshComplete();
    }
    @Override
    @Subscribe
    public void onEventMainThread(BaseEvent event) throws IOException {
        super.onEventMainThread(event);
        if (event instanceof NoticeEvent) {
            pageIndex = 0;
            messageListPost(pageIndex);
        }

    }

}
